﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;

namespace LibreTimeTracker.Library
{
  public partial class NotifyIconMenuStrip : ContextMenuStrip
  {
    //private Icon icoCheckOut;
    //private Icon icoCheckIn;
    private Image _imgCheckOut;
    private Image _imgCheckIn;
    private Window _winParent;
    private CheckInState _cis;
    private Action _windowStateChanged;

    public NotifyIconMenuStrip(Window Parent, Action WindowStateChanged)
    {
      _winParent = Parent;
      _cis = new CheckInState();

      _windowStateChanged = WindowStateChanged;

      Init();
    }

    private void Init()
    {
      _winParent.StateChanged += new EventHandler(_winParent_StateChanged);
      _cis.PropertyChanged +=new PropertyChangedEventHandler(cis_PropertyChanged);

      //icoCheckOut = new System.Drawing.Icon(Application.GetResourceStream(new Uri(URIBASE_RESOURCES, "Resources/CheckedOut.ico")).Stream);
      //icoCheckIn = new System.Drawing.Icon(Application.GetResourceStream(new Uri(URIBASE_RESOURCES, "Resources/CheckedIn.ico")).Stream);
      _imgCheckOut = Bitmap.FromFile(Path.Combine(Directory.GetCurrentDirectory(), @"..\..\Resources\CheckedOut.png"));
      _imgCheckIn = Bitmap.FromFile(Path.Combine(Directory.GetCurrentDirectory(), @"..\..\Resources\CheckedIn.png"));

      this.Items.Add("Hide", null, HideShow_Click);
      this.Items.Add("Check in", _imgCheckIn, CheckInOut_Click);
      this.Items.Add("Exit", null, Exit_Click);

      UpdateMenuItems();
    }

    private void _winParent_StateChanged(object sender, EventArgs e)
    {
      UpdateMenuItems();
    }

    private void cis_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
      UpdateMenuItems();
    }

    internal void UpdateMenuItems()
    {
      //WindowState
      if (_winParent.WindowState != WindowState.Minimized)
      {
        this.Items[0].Text = "Hide";
      }
      else
      {
        this.Items[0].Text = "Show";
      }

      //CheckedInState
      if (_cis.IsCheckedIn)
      {
 	    this.Items[1].Text = "Check out";
      this.Items[1].Image = _imgCheckOut;
      }
      else
      {
        this.Items[1].Text = "Check in";
        this.Items[1].Image = _imgCheckIn;
      }
    }

    private void HideShow_Click(object sender, EventArgs e)
    {
      _windowStateChanged();
    }

    private void CheckInOut_Click(object sender, EventArgs e)
    {
      _cis.IsCheckedIn = !_cis.IsCheckedIn; //DOTO => Replace by real time validation
    }

    private void Exit_Click(object sender, EventArgs e)
    {
      if (System.Windows.Forms.MessageBox.Show("Close Application?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
      {
        _winParent.Close();
      }
    }

  }
}
